/**
	UNDERSCORE v1.0.0

	Author: Even Simon <even.simon@gmail.com>
**/

var _ = _GET = {};

(function(_){

	/* Private vars */
	_.requestTypes = {
		text:	1,
		xml:	2,
		json:	3
	};
	_.requests = [];
	_.requestsTypes = [];
	_.requestsSuccess = [];
	_.requestsError = [];

	/* Public methods */
	_.request = function(url, params, type){
		var http = new XMLHttpRequest();

		_.requests.push(http);

		http.id = _.requests.length;

		if('undefined' == typeof(url)){
			return false;
		}

		if('undefined' == typeof(params) || !http){
			var params = {
				method:		'GET',
				async:		true,
				onSuccess:	function(){},
				onError:	function(){}
			};
		}
		else{
			params.method		=		!params.method ? 'GET' : params.method;
			params.async		=		!params.async ? true : params.async;
			params.onSuccess	=		!params.onSuccess ? function(){} : params.onSuccess;
			params.onError		=		!params.onError ? function(){} : params.onError;
		}

		var post = '';
		if(params.post){
			for(var param in params.post){
				post += '&' + param + '=' + encodeURIComponent(params.post[param]);
			}
		}

		_.requestsTypes[http.id] = 'undefined' == typeof(type) ? _.requestTypes.text : type;
		_.requestsSuccess[http.id] = params.onSuccess;
		_.requestsError[http.id] = params.onError;

		http.open(params.method, url, params.async);

		http.onreadystatechange = function(){
			if(this.readyState == 4){
				if(this.status == 200){
					switch(_.requestsTypes[this.id]){
						case _.requestTypes.text:
							(_.requestsSuccess[this.id])(this.responseText);
						break;
						case _.requestTypes.xml:
							(_.requestsSuccess[this.id])(this.responseXML);
						break;
						case _.requestTypes.json:
							(_.requestsSuccess[this.id])(eval(this.responseText));
						break;
					}
				}
				else{
					(_.requestsError[this.id])(this.statusText);
				}
			}
		};

		http.send(post.substring(1));

		return http;
	};

	_.getXml = function(url, callback){
		return _.request(
			url,
			{
				onSuccess:callback				
			},
			_.requestTypes.xml
		);
	};

	_.getJson = function(url, callback){
		if('undefined' == typeof(url)){
			return false;
		}

		if(_.isSameDomain(url)){
			// XML request
			_.request(
				url,
				{
					onSuccess:callback
				},
				_.requestTypes.json
			);

			return true;
		}
		else{
			// Script request
			if('undefined' != typeof(callback)){
				url += (url.indexOf('?') != -1 ? '&' : '?') + encodeURIComponent(callback);
			}

			var script = document.createElement('script');
			script.setAttribute('type', 'text/javascript');
			script.setAttribute('src', url);
			document.getElementsByTagName('head')[0].appendChild(script);

			return true;
		}
	};

	_.getData = function(url, callback){
		return _.request(
			url,
			{
				onSuccess:callback
			},
			_.requestTypes.text
		);
	};

	_.isSameDomain = function(url){
		if('undefined' == typeof(url)){
			return false;
		}

		var thisDomain = document.location.protocol + '//' + document.location.host + '/';
		return url.toString().substr(0,thisDomain.length) == thisDomain;
	};

	/* Core */

		/* Parse query string params */
		var params = [];
		var qs = document.location.search.toString().split('&');
		for(var i in qs){
			var param = qs[i].split('=');
			params[param[0]] = param[1];
		}
		_GET = params;


		/* Assign Microsoft XMLHttpRequests if XMLHttpRequest object does not exist */
		if('undefined' == typeof(window.XMLHttpRequest)){
			try {
				xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				try{
					xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
				}catch(E){
					xmlhttp = false;
				}
			}
			window.XMLHttpRequest = xmlhttp;
		}
})(_);

var XMLObject = function(object){

	/* Create XML object from string/object */
	if('string' == typeof(object)){
		if (window.DOMParser){
			var parser = new DOMParser();
			this._object = parser.parseFromString(object, 'text/xml');
		}
		else{
			this._object = new ActiveXObject('Microsoft.XMLDOM');
			this._object.async = 'false';
			this._object.loadXML(object); 
		}
	}
	else{
		this._object = object;
	}

	/* Public methods */

		/* Get the original XML object */
		this._get = function(){
			return this._object;
		};

		this.search = function(query){
			var data = this._object.getElementsByTagName(query);

			return !data || !data.length ? false : new XMLObject(data);
		};

		this.xPath = function(query){
			
		};

	/* Private methods */

	
};